﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BLL;
using System.ServiceModel;

namespace DAL
{
    /// <summary>
    /// Use EntityFramework to Implement AuthorityRepository
    /// </summary>
    public class AuthorityRepository : IAuthorityRepository
    {
        WorkShopEntities dbContext = new WorkShopEntities();
        /// <summary>
        /// Save a New Authority to Database
        /// </summary>
        /// <param name="authorityBE"></param>
        /// <returns></returns>
        public bool AddNewAuthority(BLL.Authority authorityBE)
        {
            DAL.Authority authorityDE = Translator.AuthorityTranslator.TranslateBusinessToEntity(authorityBE);
            dbContext.AddToAuthority(authorityDE);
            dbContext.SaveChanges();
            return true;
        }

        /// <summary>
        /// Delete Authority from Database
        /// </summary>
        /// <param name="AuthorityId"></param>
        /// <returns></returns>
        public bool DeleteAuthority(int AuthorityId)
        {
            DAL.Authority authorityDE = (from Authority a in dbContext.Authority where a.AuthorityId == AuthorityId select a).SingleOrDefault<Authority>();
            if (authorityDE == null)
            {
                throw new FaultException("This authority has been deleted.");
            }
            List<DAL.Role> roleList = authorityDE.Role.ToList<Role>();
            if (roleList.Count == 0)
            {
                dbContext.Authority.DeleteObject(authorityDE);
                dbContext.SaveChanges();
                return true;
            }

            else
            {
                throw new FaultException("This data is in use now.");
            }

        }

        /// <summary>
        /// Update Authority to Database
        /// </summary>
        /// <param name="authorityBE"></param>
        /// <returns></returns>
        public bool UpdateAuthority(BLL.Authority authorityBE)
        {
            DAL.Authority authorityDE = Translator.AuthorityTranslator.TranslateBusinessToEntity(authorityBE);
            authorityDE = (from Authority a in dbContext.Authority where a.AuthorityId == authorityDE.AuthorityId select a).SingleOrDefault<Authority>();
            if (authorityDE == null)
            {
                throw new FaultException("This data has been deleted.");
            }
            else
            {
                authorityDE.AuthorityName = authorityBE.AuthorityName;
                authorityDE.LevelId = authorityBE.Level.LevelId;
                dbContext.SaveChanges();
            }
            return true;
        }

        /// <summary>
        /// Get AuthorityName by Current AuthorityId
        /// </summary>
        /// <param name="authorityId"></param>
        /// <returns></returns>
        public BLL.Authority GetAuthorityById(int authorityId)
        {
            DAL.Authority authorityDE = (from Authority a in dbContext.Authority where a.AuthorityId == authorityId select a).SingleOrDefault<Authority>();
            if (authorityDE == null)
            {
                throw new FaultException("This data has been deleted.");
            }
            else
            {
                BLL.Authority authorityBE = Translator.AuthorityTranslator.TranslateEntityToBusiness(authorityDE);
                return authorityBE;
            }

        }

        /// <summary>
        /// Get All Authority Level
        /// </summary>
        /// <returns></returns>
        public List<BLL.AuthorityLevel> GetAllAuthorityLevel()
        {
            List<DAL.AuthorityLevel> listDE = (from AuthorityLevel l in dbContext.AuthorityLevel select l).ToList<AuthorityLevel>();
            List<BLL.AuthorityLevel> authorityLevelListBE = new List<BLL.AuthorityLevel>();
            foreach (AuthorityLevel levelDE in listDE)
            {
                BLL.AuthorityLevel levelBE = Translator.AuthorityLevelTranslator.TranslateEntityToBusiness(levelDE);
                authorityLevelListBE.Add(levelBE);
            }
            return authorityLevelListBE;
        }

        /// <summary>
        /// Get All Authority and Level Details
        /// </summary>
        /// <returns></returns>
        public List<BLL.Authority> GetAllAuthority()
        {
            List<DAL.Authority> listDE = (from a in dbContext.Authority join l in dbContext.AuthorityLevel on a.AuthorityLevel.LevelId equals l.LevelId select a).ToList<Authority>();
            List<BLL.Authority> authorityAndLevelListBE = new List<BLL.Authority>();
            foreach (Authority authorityDE in listDE)
            {
                BLL.Authority authorityBE = Translator.AuthorityTranslator.TranslateEntityToBusiness(authorityDE);
                authorityAndLevelListBE.Add(authorityBE);

            }
            return authorityAndLevelListBE;
        }
        public List<BLL.Authority> SearchAuthority(string authorityName, int levelId)
        {
            List<DAL.Authority> listDE = (from a in dbContext.Authority where a.AuthorityName.Contains(authorityName) && (a.AuthorityLevel.LevelId == levelId || levelId == 0) select a).ToList<Authority>();
            List<BLL.Authority> authorityListBE = new List<BLL.Authority>();
            foreach (Authority authorityDE in listDE)
            {
                BLL.Authority authorityBE = Translator.AuthorityTranslator.TranslateEntityToBusiness(authorityDE);
                authorityListBE.Add(authorityBE);

            }
            return authorityListBE;
        }
    }
}
